<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>实现 strStr()</title>
  </head>
  <body>
    <script>
      var strStr = function (haystack, needle) {
        // if (needle == "") {
        //   return 0;
        // }
        // return haystack.indexOf(needle);
        const hayLength = haystack.length;
        const needLength = needle.length;
        var str = "";
        if (needLength === 0) {
          return 0;
        }
        for (let i = 0; i < hayLength; i++) {
          if (haystack[i] === needle[0]) {
            str = haystack.slice(i, i + needLength);
            if (str === needle) {
              return i;
            }
          }
        }
        return -1;
      };
      var haystack = "hello",
        needle = "ll";
      console.log(strStr(haystack, needle));
    </script>
  </body>
</html>
